var util = require('../../utils/util.js');
var ajax = require('../../utils/ajax.js');
var common = require('../../utils/common.js');
var WxParse = require('../../utils/wxParse/wxParse.js');
var MPage = require('../MPage.js');
var app = getApp();

Page(common.extendPage(MPage, {

    /**
     * 页面的初始数据
     */
    data: {
        isAllSelected: true,
        allTotalPrice: 0,
        cardInfo: '',
        userCard: '',
        cid: '',
        cardId: '',
        num: '',
        // haveCard: false,
        dataLoaded: false,
        isLogin: !!app.getUserSid()
    },

    onLoad: function(options) {

        this.setData({
            cardId: options.cardId,
            // haveCard: eval(options.haveCard)
        });
    },

    onShow: function() {
        this.setData({
            isLogin: !!app.getUserSid()
        })
        this.getMembershipCard();
    },
    onLoginSuc() {
        this.setData({
            isLogin: true
        })
        this.getMembershipCard();
    },
    _showLogin() {
        this.setData({
            isLoginShow: true
        });
    },

    _goNext: function(evt) {
        if (!this.data.isLogin) {
            this._showLogin();
            return;
        }
        common.jump(evt);
    },
    getMembershipCard: function() {
        var _self = this;
        var cardId = this.data.cardId;
        ajax.P('/openapi/CUser/cardInfo', {
                cardId: cardId
            },
            function(data) {
                var data = data.result;

                if (data['userCard']) {
                    data['userCard']['cid'] = _self.PrefixInteger(data['userCard']['cid'], 6);
                    _self.setData({
                        cardInfo: data.cardInfo,
                        userCard: data.userCard,
                        dataLoaded: true
                    })

                } else {

                    _self.setData({
                        cardInfo: data,
                        dataLoaded: true
                    })
                }
            })
    },
    PrefixInteger: function(num, length) {
        return (Array(length).join('0') + num).slice(-length);
    },
    _jump: function(evt) {
        common.jump(evt);
    }
}))